package com.parachutes.aspsystemweb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.parachutes.aspinterfaceapi.domain.BusInterface;
import com.parachutes.aspinterfaceapi.domain.InterfaceFeign;
import com.parachutes.aspsystemapi.domian.Result;
import com.parachutes.aspsystemapi.domian.SysUser;
import com.parachutes.aspsystemapi.domian.UserInterfaceSubscribe;
import com.parachutes.aspsystemapi.domian.group.LoginGroup;
import com.parachutes.aspsystemapi.domian.group.RegisterGroup;
import com.parachutes.aspsystemapi.feign.UserInterfaceSubscribeService;
import com.parachutes.aspsystemapi.service.SysUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * SYS 用户控制器
 *
 * @author machi
 * @date 2025/01/23
 */
@RestController
@RequestMapping("user")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;


    /**
     * 登录
     *
     * @param sysUser SYS 用户
     * @return {@link Result }<{@link ? }>
     */
    @PostMapping("login")
    public Result<?> login(@Validated(LoginGroup.class) @RequestBody SysUser sysUser) {
        LambdaQueryWrapper<SysUser> wrapper = Wrappers.lambdaQuery(SysUser.class).eq(SysUser::getUsername, sysUser.getUsername());
        SysUser user = sysUserService.getOne(wrapper);
        if (user == null) {
            return Result.of(false, "用户名或密码错误");
        }
        if (!user.getPassword().equals(sysUser.getPassword())) {
            return Result.of(false, "用户名或密码错误");
        }
        return Result.of(true, "登录成功", user);
    }

    @PostMapping("register")
    public Result<?> register(@Validated(RegisterGroup.class) @RequestBody SysUser sysUser) {
        //邮箱合法性验证
        String email = sysUser.getEmail();
        if (!email.matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$")) {
            return Result.of(false, "邮箱格式不正确");
        }
        //对象是否已存在
        LambdaQueryWrapper<SysUser> wrapper = Wrappers.lambdaQuery(SysUser.class).eq(SysUser::getUsername, sysUser.getUsername());
        SysUser user = sysUserService.getOne(wrapper);
        if (user != null) {
            return Result.of(false, "用户名已存在");
        }
        //邮箱
        //保存用户
        sysUserService.save(sysUser);
        return Result.of(true, "注册成功");
    }
    /**
     * 获取用户
     *
     * @param id 身份证
     * @return {@link Result }<{@link ? }>
     */
    @GetMapping
    public Result<?> getUser(@RequestParam Integer id) {
        SysUser user = sysUserService.getById(id);
        if (user == null) {
            return Result.of(false, "用户不存在");
        }
        return Result.of(true, "获取成功", user);
    }

    /**
     * 更新用户
     *
     * @param sysUser SYS 用户
     * @return {@link Result }<{@link ? }>
     */
    @PutMapping
    public Result<?> updateUser(@Validated @RequestBody SysUser sysUser) {
        sysUserService.updateById(sysUser);
        return Result.of(true, "更新成功");
    }

    /**
     * 列出用户
     *
     * @return {@link Result }<{@link ? }>
     */
    @GetMapping("list")
    public Result<?> listUser(Integer page, Integer pageSize) {
        PageMethod.startPage(page, pageSize);
        List<SysUser> list = sysUserService.list();
        PageInfo<SysUser> pageInfo = new PageInfo<>(list);
        return Result.of(true, "获取成功", pageInfo);
    }


}
